package com.wl.mapper;

import com.wl.pojo.InterestTag;
import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Personal.CollectContent;
import com.wl.pojo.Personal.Personal;
import com.wl.pojo.Personal.PersonalUser;
import com.wl.pojo.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PersonalMapper {

    /**
     * 获取用户的基本信息，包括兴趣标签
     */
    // 查询基本信息
    @Select("SELECT\n" +
            "    u.user_id,\n" +
            "    u.username,\n" +
            "    u.img_url,\n" +
            "    u.intro,\n" +
            "    u.create_time,\n" +
            "    COUNT(DISTINCT f.follow_id) AS following_count,  -- 关注的人数\n" +
            "    COUNT(DISTINCT f2.user_id) AS followers_count,  -- 粉丝的人数\n" +
            "    COUNT(DISTINCT l.post_id) AS likes_count  -- 文章总点赞次数\n" +
            "FROM\n" +
            "    users u\n" +
            "        LEFT JOIN\n" +
            "    user_follow f ON u.user_id = f.user_id  -- 关注的人\n" +
            "        LEFT JOIN\n" +
            "    user_follow f2 ON u.user_id = f2.follow_id  -- 粉丝\n" +
            "        LEFT JOIN\n" +
            "    posts p ON u.user_id = p.user_id\n" +
            "        LEFT JOIN\n" +
            "    likes l ON p.post_id = l.post_id\n" +
            "where u.user_id = #{userId};")
    Personal getPersonalInfo(Integer userId);
    // 查询兴趣标签
    @Select("select it.tags_name from users u\n" +
            "join user_tag on u.user_id = user_tag.user_id\n" +
            "join interest_tags it on user_tag.interest_id = it.interest_id\n" +
            "where u.user_id = #{userId}")
    List<String> getUserTags(Integer userId);
    // 查询文章被浏览量
    @Select("select sum(page_view) from posts where user_id = #{userId}")
    Integer getPostPageViews(Integer userId);

    /**
     * 分页获取关注的情况，包括获取粉丝和获取关注的用户两种情况
     * currentSwitch 用来判断是查询关注还是查询粉丝
     */
    // 查询关注信息
    @Select("select u.user_id, u.username, u.img_url, u.intro from users u\n" +
            "join user_follow uf on u.user_id = uf.follow_id\n" +
            "where uf.user_id = #{userId}")
    List<Personal> getFollow(Integer userId);
    // 查询粉丝信息
    @Select("select u.user_id, u.username, u.img_url, u.intro from users u\n" +
            "join user_follow uf on u.user_id = uf.user_id\n" +
            "where uf.follow_id = #{userId}")
    List<Personal> getFans(Integer userId);

    /**
     * 获取当前登陆用户的关注用户的id列表
     */
    @Select("select u.user_id from users u join user_follow uf on u.user_id = uf.follow_id where uf.user_id = #{userId};")
    List<Integer> getLoginUserFollow(Integer userId);

    /**
     * 获取个人主页页面的展示的文章内容
     */
    List<Post> getPersonalPost(String activeName, Integer userId);
    @Select("select tag_name as tag_names from post_tag pt join tags t on pt.tag_id = t.tag_id where pt.post_id = #{postId}")
    List<String> getPostTagNames(Integer postId);
    @Select("select tag_id as tag_ids from post_tag pt where pt.post_id = #{postId}")
    List<Integer> getPostTagIds(Integer postId);

    /**
     * 删除文章操作
     * @param postId 文章的id
     */
    // 删除文章点赞表中的信息
    @Delete("delete from likes where post_id = #{postId}")
    void deletePostLikesByPostId(Integer postId);
    // 删除文章收藏表中的信息
    @Delete("delete from collect where post_id = #{postId}")
    void deletePostCollectByPostId(Integer postId);
    // 删除评论的点赞信息
    @Delete("delete from comment_likes where comment_id in (select id from comments where post_id = #{postId})")
    void deleteCommentLikesByPostId(Integer postId);
    // 删除文章的评论
    @Delete("delete from comments where post_id = #{postId}")
    void deleteCommentByPostId(Integer postId);
    // 删除文章
    @Delete("delete from posts where post_id = #{postId}")
    void deletePostById(Integer postId);

    /**
     * 获取用户可供选择的兴趣标签数据
     */
    @Select("select * from interest_tags")
    List<InterestTag> getInterest();

    /**
     * 修改个人信息的查询方法
     * 查询个人信息，包括详细的兴趣标签信息
     */
    // 查询基本信息
    @Select("select * from users where user_id = #{userId}")
    PersonalUser getUserInfo(Integer userId);
    // 查询兴趣标签信息
    @Select("select it.interest_id,it.tags_name from users u\n" +
            "join user_tag ut on u.user_id = ut.user_id\n" +
            "join interest_tags it on ut.interest_id = it.interest_id\n" +
            "where u.user_id = #{userId};")
    List<InterestTag> getUserTagsData(Integer userId);

    /**
     * 修改用户信息，仅包括用户名，个人介绍。兴趣标签
     */
    // 删除用户兴趣标签数据
    @Delete("delete from user_tag where user_id = #{userId}")
    void deleteUserTagsByUserId(Integer userId);
    // 重新更新用户标签数据
    void setUserTags(Integer userId, List<Integer> userTags);
    // 更新基本信息
    @Update("update users set username = #{username},img_url = #{imgUrl},intro = #{intro} where user_id = #{userId}")
    void updateUserInfo(PersonalUser personalUser);

    /**
     * 修改密码接口
     */
    @Update("update users set password = #{password} where user_id = #{userId}")
    void updatePassword(Integer userId, String password);

    /**
     * 获取收藏的数据
     * @param userId 用户ID
     * @return 返回收藏数据
     */
    // 获取收藏夹名、更新时间、内容数量
    @Select("select cc.*, (\n" +
            "    select count(c.category_id)\n" +
            "    from collect c\n" +
            "    where c.category_id = cc.category_id\n" +
            ") as count\n" +
            "from collect_category cc\n" +
            "where cc.user_id = #{userId};")
    List<Collect> getCollectData(Integer userId);
    // 获取每个收藏夹收藏的内容
    @Select("select p.post_id, p.title from posts p join collect c on p.post_id = c.post_id\n" +
            "where c.category_id = #{categoryId};")
    List<CollectContent> getCollectContentData(Integer categoryId);

    /**
     * 删除收藏夹
     */
    // 删除收藏夹中的内容
    @Delete("delete from collect where category_id = #{categoryId};")
    void deleteCollectByCategoryId(Integer categoryId);
    // 删除收藏夹
    @Delete("delete from collect_category where category_id = #{categoryId};")
    void deleteCategoryById(Integer categoryId);

    /**
     * 更新收藏夹名称
     */
    @Update("update collect_category set category_name = #{categoryName}, is_private = #{isPrivate}" +
            " where category_id = #{categoryId};")
    void updateCollectCategory(Collect collect);
}
